package com.cskaoyan.service;

import com.cskaoyan.bean.Footprint;
import com.cskaoyan.bean.FootprintExample;
import com.cskaoyan.mapper.FootprintMapper;
import com.cskaoyan.vo.FootprintVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能:会员足迹页面
 * 作者:whj
 */
@Service
public class FootprintServicelmpl implements FootprintService {

    @Autowired
    FootprintMapper footprintMapper;

    @Override
    public FootprintVo query(Integer page, Integer limit, Integer userId, Integer goodsId, String sort, String order) {
        //分页
        PageHelper.startPage(page,limit);
        //排序
        FootprintExample footprintExample = new FootprintExample();
        footprintExample.setOrderByClause(sort + " " + order);
        //根据userId,valueId进行精准查询
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if(userId != null && !"".equals(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        if(goodsId != null && !"".equals(goodsId)){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        //符合条件的total
        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        long total = footprintPageInfo.getTotal();
        //返回结果
        FootprintVo footprintVo = new FootprintVo();
        footprintVo.setItems(footprints);
        footprintVo.setTotal(total);
        return footprintVo;
    }
}
